﻿using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;

namespace Yawf.Core.Utils {
	[TestFixture]
	public class StringUtilsTest {
		[Test]
		public void TestSplitString() {
			String[] result;

			result = StringUtils.SplitString("Hola,Hej;Hello Fucking!World",",;!");

			Assert.IsNotNull(result,"result is null");
			Assert.AreEqual(result.Length,4,"result.Length != 4");
			Assert.AreEqual(result[0],"Hola","result[0] != 'Hola'");
			Assert.AreEqual(result[1],"Hej","result[1] != 'Hej'");
			Assert.AreEqual(result[2],"Hello Fucking","result[2] != 'Hello Fucking'");
			Assert.AreEqual(result[3],"World","result[3] != 'World'");

			result = StringUtils.SplitString("Hola,Hej",',');
			Assert.IsNotNull(result,"result is null");
			Assert.AreEqual(result.Length,2,"result.Length != 2");
			Assert.AreEqual(result[0],"Hola","result[0] != 'Hola'");
			Assert.AreEqual(result[1],"Hej","result[1] != 'Hej'");
		}

		[Test]
		public void TestConcatenate() {
			String result;

			result = StringUtils.Concatenate(new String[] { "Hola","Hej","Hello","Tjaba" },',');
			Assert.IsNotNull(result,"result is null");
			Assert.AreEqual(result,"Hola,Hej,Hello,Tjaba","result != 'Hola,Hej,Hello,Tjaba'");
		}

		[Test]
		public void TestFormarRUT() {
			String result;

			result = StringUtils.FormarRUT("8310082","6");
			Assert.IsNotNull(result,"result is null");
			Assert.AreEqual(result,"8.310.082-6");

			result = StringUtils.FormarRUT("83100826");
			Assert.IsNotNull(result,"result is null");
			Assert.AreEqual(result,"8.310.082-6");
		}

		[Test]
		public void TestBoolValue() {
			Assert.IsTrue(StringUtils.BoolValue("yes"));
			Assert.IsTrue(StringUtils.BoolValue("Y"));
			Assert.IsTrue(StringUtils.BoolValue("si"));
			Assert.IsTrue(StringUtils.BoolValue("s"));
			Assert.IsTrue(StringUtils.BoolValue("True"));
			Assert.IsTrue(StringUtils.BoolValue("T"));
			Assert.IsTrue(StringUtils.BoolValue("t"));
			Assert.IsTrue(StringUtils.BoolValue("oN"));
			Assert.IsFalse(StringUtils.BoolValue(null));
			Assert.IsFalse(StringUtils.BoolValue(""));
			Assert.IsFalse(StringUtils.BoolValue("hola"));
		}

		[Test]
		public void TestIsEmpty() {
			Assert.IsTrue(StringUtils.IsEmpty(null));
			Assert.IsTrue(StringUtils.IsEmpty(""));
			Assert.IsTrue(StringUtils.IsEmpty("        "));
			Assert.IsFalse(StringUtils.IsEmpty("    s    "));
			Assert.IsFalse(StringUtils.IsEmpty("Hola"));
			Assert.IsTrue(StringUtils.IsEmpty("\t\r\n"), "StringUtils.IsEmpty(\\t\\r\\n) is false");
		}

		[Test]
		public void TestFindStringIndex() {
			IList<String> testList;
			int index;

			testList = new List<String>();
			testList.Add("name");
			index = StringUtils.FindStringIndex(testList,"name",2,".");
			Assert.AreEqual(-1,index,"index should be -1 and its not");

			testList.Add("name.prop1");
			index = StringUtils.FindStringIndex(testList,"name",2,".");
			Assert.AreEqual(1,index,"index should be 1, but is is not");

			testList.Add("name.prop1.prop2");
			index = StringUtils.FindStringIndex(testList,"name",3,".");
			Assert.AreEqual(2,index,"index should be 2, but is is not");
		}

	}
}
